<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            list-style: none;
        }

        ul li {
            width: 500px;
            height: 50px;
            line-height: 50px;
        }

        ul li span {
            display: inline-block;
            width: 100px;
            text-align: center;
            line-height: 50px;
        }

        ul li button {
            width: 30px;
        }
    </style>

</head>

<body>

    <!-- 
        购物车： + ， - ，全选， 反选， 总价
     -->
    <div id="app">
        <ul>
            <li v-for="(item, index) in list" :key="item.id">
                <input type="checkbox" name="" id="" v-model="item.flag">
                <span>{{item.title}}</span>
                <span>{{item.price}}</span>
                <span>
                    <button @click="item.num--" :disabled="item.num === 1">-</button>
                    {{item.num}}
                    <button @click="item.num++">+</button>
                </span>
            </li>
        </ul>
        <p>
            <input type="checkbox" name="" id="" v-model="allchecked">
            总价：{{totalPrice}}
        </p>
    </div>
</body>

<script>
    const { createApp } = Vue

    const vm = createApp({
        data() {
            return {
                list: [
                    {
                        price: 10,
                        title: '商品1号',
                        id: 1,
                        flag: false,
                        num: 1
                    },
                    {
                        price: 20,
                        title: '商品2号',
                        id: 2,
                        flag: false,
                        num: 2
                    },
                    {
                        price: 30,
                        title: '商品3号',
                        id: 3,
                        flag: false,
                        num: 3
                    },
                ]
            }
        },
        computed: {

            allchecked: {
                get() {
                    return this.list.every(item => item.flag)
                },
                set(val) {
                    return val ? this.list.forEach(item => item.flag = true) : this.list.forEach(item => item.flag = false)
                }
            },
            totalPrice() {
                let sum = 0
                this.list.forEach(item => item.flag ? sum += item.price * item.num : 0)
                return sum
            }
        }
    }).mount('#app')


</script>

</html>